package org.dromara.application.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.application.domain.vo.AppTypeVo;
import org.dromara.application.domain.bo.AppTypeBo;
import org.dromara.application.service.IAppTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 应用类型
 *
 * @author YCJZ
 * @date 2025-09-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/application/appType")
public class AppTypeController extends BaseController {

    private final IAppTypeService appTypeService;

    /**
     * 查询应用类型列表
     */
    @SaCheckPermission("application:appType:list")
    @GetMapping("/list")
    public TableDataInfo<AppTypeVo> list(AppTypeBo bo, PageQuery pageQuery) {
        return appTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应用类型列表
     */
    @SaCheckPermission("application:appType:export")
    @Log(title = "应用类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppTypeBo bo, HttpServletResponse response) {
        List<AppTypeVo> list = appTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "应用类型", AppTypeVo.class, response);
    }

    /**
     * 获取应用类型详细信息
     *
     * @param appTypeId 主键
     */
    @SaCheckPermission("application:appType:query")
    @GetMapping("/{appTypeId}")
    public R<AppTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long appTypeId) {
        return R.ok(appTypeService.queryById(appTypeId));
    }

    /**
     * 新增应用类型
     */
    @SaCheckPermission("application:appType:add")
    @Log(title = "应用类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppTypeBo bo) {
        return toAjax(appTypeService.insertByBo(bo));
    }

    /**
     * 修改应用类型
     */
    @SaCheckPermission("application:appType:edit")
    @Log(title = "应用类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppTypeBo bo) {
        return toAjax(appTypeService.updateByBo(bo));
    }

    /**
     * 删除应用类型
     *
     * @param appTypeIds 主键串
     */
    @SaCheckPermission("application:appType:remove")
    @Log(title = "应用类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] appTypeIds) {
        return toAjax(appTypeService.deleteWithValidByIds(List.of(appTypeIds), true));
    }
}
